package com.lb.jwitter.action;


import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Map; 
import org.springframework.test.context.ContextConfiguration; 
import com.lb.jwitter.service.TblQuyenChiTietService; 
import com.lb.jwitter.service.TblTaiKhoan; 
import com.opensymphony.xwork2.ActionContext; 
import org.springframework.context.ApplicationContext;
import com.lb.jwitter.util.ApplicationContextProvider; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Date; 
import java.util.GregorianCalendar; 
import javax.xml.datatype.DatatypeFactory; 
import javax.xml.datatype.XMLGregorianCalendar; 
import com.lb.jwitter.service.ConditionReport; 
import com.lb.jwitter.util.Message; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.TblCTDTNhomMHTuChonMonHoc;
import com.lb.jwitter.service.TblCTDTNhomMHTuChonMonHocService;
import com.lb.jwitter.service.TblCTDTNhomMHTuChon;
import com.lb.jwitter.service.TblCTDTNhomMHTuChonService;

import com.lb.jwitter.service.TblCTDTMonHoc;
import com.lb.jwitter.service.TblCTDTMonHocService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblCTDTNhomMHTuChontblCTDTNhomMHTuChonMonHocInsertAction{
    private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    public void setPageSize(int pageSize) { this.pageSize = pageSize; }

    private int page; 
    public int getPage() { return page; }

    public void setPage(int page) { this.page = page; }

    private int pages; 
    public int getPages() { return pages; }

    public void setPages(int pages) { this.pages = pages; }

    private String dataString; 
    public String getDataString() { return dataString; }

    private Message msg; 
    public Message getMsg() { return msg; }

    private Long id; 
    public void setId(Long id) {
       this.id = id;
 }

    private int sTT; 
    public void setSTT(int sTT) {
       this.sTT = sTT;
 }

    public int getSTT() {
       return sTT;
 }

    private int nhomMHTCId; 
    public void setNhomMHTCId(int nhomMHTCId) {
       this.nhomMHTCId = nhomMHTCId;
 }

    public int getNhomMHTCId() {
       return nhomMHTCId;
 }

    private TblCTDTNhomMHTuChonService tblCTDTNhomMHTuChonService; 

    private int monHocId; 
    public void setMonHocId(int monHocId) {
       this.monHocId = monHocId;
 }

    public int getMonHocId() {
       return monHocId;
 }

    private TblCTDTMonHocService tblCTDTMonHocService; 

    private TblCTDTNhomMHTuChonMonHoc tblCTDTNhomMHTuChonMonHoc; 
    public TblCTDTNhomMHTuChonMonHoc  getTblCTDTNhomMHTuChonMonHoc() {
        return tblCTDTNhomMHTuChonMonHoc;
 }

    private List<TblCTDTNhomMHTuChonMonHoc> tblCTDTNhomMHTuChonMonHocs; 
    public List<TblCTDTNhomMHTuChonMonHoc>  getTblCTDTNhomMHTuChonMonHocs() {
        return tblCTDTNhomMHTuChonMonHocs; 
      } 

    private TblCTDTNhomMHTuChonMonHocService tblCTDTNhomMHTuChonMonHocService; 

	private int preInsert(Message msg, TblCTDTNhomMHTuChonMonHoc obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postInsert(Message msg, TblCTDTNhomMHTuChonMonHoc obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
    public String execute() throws Exception 
   { 
    try{ 
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_CTDT_NhomMHTuChon_MonHoc", "them")){ 
        return "noprivilege"; 
    }
        tblCTDTNhomMHTuChonMonHocService = (TblCTDTNhomMHTuChonMonHocService) context.getBean("tbl_CTDT_NhomMHTuChon_MonHocClient"); 
        tblCTDTNhomMHTuChonService = (TblCTDTNhomMHTuChonService) context.getBean("tbl_CTDT_NhomMHTuChonClient"); 

        tblCTDTMonHocService = (TblCTDTMonHocService) context.getBean("tbl_CTDT_MonHocClient"); 

        msg = new Message(); 
        tblCTDTNhomMHTuChonMonHoc = new TblCTDTNhomMHTuChonMonHoc(); 
        tblCTDTNhomMHTuChonMonHoc.setSTT(sTT); 
        tblCTDTNhomMHTuChonMonHoc.setNhomMHTCId(tblCTDTNhomMHTuChonService.gettblCTDTNhomMHTuChon(new Long(nhomMHTCId))); 
        tblCTDTNhomMHTuChonMonHoc.setMonHocId(tblCTDTMonHocService.gettblCTDTMonHoc(new Long(monHocId))); 
        preInsert(msg,tblCTDTNhomMHTuChonMonHoc); 
       if (msg.isPreMessage_b()) 
       { 
           tblCTDTNhomMHTuChonMonHocService.createtblCTDTNhomMHTuChonMonHoc(tblCTDTNhomMHTuChonMonHoc); 
           postInsert(msg, tblCTDTNhomMHTuChonMonHoc); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 
       if(pageSize == 0){ 
           pageSize = 20; 
       } 
        if(page == 0){ 
           page =1; 
       } 
        List < ConditionReport > cons = new ArrayList<ConditionReport>(); 
        ConditionReport con = new ConditionReport(); 
        con.setCol("nhomMHTCId.id"); 
        con.setValue1(new Long(nhomMHTCId)); 
        con.setOperator("eq"); 
        cons.add(con); 
      tblCTDTNhomMHTuChonMonHocs =       tblCTDTNhomMHTuChonMonHocService.findtblCTDTNhomMHTuChonMonHocByCondition(cons,pageSize, page); 
      pages = tblCTDTNhomMHTuChonMonHocService.counttblCTDTNhomMHTuChonMonHocByCondition(cons)/pageSize + 1; 
      dataString = ""; 
      dataString += "&id="; 
      dataString += "&sTT="; 
      dataString += "&nhomMHTCId="; 
      dataString += "&monHocId="; 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Insert tbl_CTDT_NhomMHTuChon_MonHoc Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
 } 
 
